<template>
  <van-icon
    :color="collected?'#e22829':'#777'"
    :name="collected?'star':'star-o'"
    @click="onCollected"
  />
</template>

<script>
import { addCollected, deleteCollected } from '@/api/article.js'
export default {
  props: {
    collected: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, Object],
      required: true
    }
  },
  data () {
    return {}
  },
  methods: {
    async onCollected () {
      // loading加载提示
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        message: '加载中...',
        forbidClick: true
      })
      try {
        if (this.collected) {
          // 已经收藏 进行取消收藏操作
          await deleteCollected(this.articleId)
        } else {
          // 进行收藏操作
          await addCollected(this.articleId)
        }
        this.$toast('操作成功')
        this.$emit('input', !this.collected)
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  model: {
    // 属性解析为collected
    prop: 'collected'
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
